//
//  NewsModels.m
//  student
//
//  Created by JCai on 2018/9/6.
//  Copyright © 2018年 haiger. All rights reserved.
//

#import "NewsModels.h"
#import "ModelUtil.h"

@implementation NewsModels

- (instancetype)initWithDict:(NSDictionary *)dict
{
    self = [super init];
    if (self) {
        self.message_id = [dict objectForKey:@"id"];
        self.imgPath = [ModelUtil changeToString:[dict objectForKey:@"imgPath"]];
        self.senderName = [dict objectForKey:@"senderName"];
        self.messageTitle = [dict objectForKey:@"messageTitle"];
        self.messageContent = [dict objectForKey:@"messageContent"];
        self.is_read = [ModelUtil changeToInteger:[dict objectForKey:@"isRead"]];
        self.sendTime = [ModelUtil changeToString:[dict objectForKey:@"sendTime"]];
        self.senderType = [ModelUtil changeToInteger:[dict objectForKey:@"senderType"]];
        self.mainId = [ModelUtil changeToInteger:[dict objectForKey:@"mainId"]];
        self.recvUserId = [ModelUtil changeToInteger:[dict objectForKey:@"recvUserId"]];
    }
    return self;
}

- (NSString *)getMonthDay
{
    NSString *sendTime = @"";
    if (self.sendTime.length > 0) {
        sendTime = [self.sendTime substringWithRange:NSMakeRange(5, 5)];
    }
    
    return sendTime;
}

- (NSString *)weekdayStringFromDate:(NSString *)timeString {
    NSDateFormatter *formatter1 = [[NSDateFormatter alloc]init];
    [formatter1 setDateFormat:@"yyyy-MM-dd HH-mm-sss"];
    NSDate *inputDate = [formatter1 dateFromString:timeString];
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"星期日", @"星期一", @"星期二", @"星期三", @"星期四", @"星期五", @"星期六", nil];
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    [calendar setTimeZone: timeZone];
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    return [weekdays objectAtIndex:theComponents.weekday];
}

- (NSString *)getWeekDay
{
    NSString *sendTime = @"";
    if (self.sendTime.length > 0) {
        NSString *month = [self.sendTime substringWithRange:NSMakeRange(5, 2)];
        NSString *day = [self.sendTime substringWithRange:NSMakeRange(8, 2)];
        NSString *week = [self weekdayStringFromDate:self.sendTime];
        sendTime = [NSString stringWithFormat:@"%@月%@日 %@", @([month integerValue]), @([day integerValue]), week];
    }
    
    return sendTime;
}

- (NSString *)getTimeString
{
    NSString *sendTime = @"";
    if (self.sendTime.length > 0) {
        sendTime = [self.sendTime substringWithRange:NSMakeRange(11, 5)];
    }
    
    return sendTime;
}
@end
